* merge_ocupados.do
* This file merges the clean "ocupados" file into a single dataset to be saved in the source folder.
* Inputs: General, Fuerza de trabajo  and ocupados files in C:/Users/JorgeEduardo/Dropbox/AA_Minimum_Wage/Data/Clean/ENH
* Outputs: /Data/Source/ocupados.dta

clear all
version 13.1

* Project info
project, doinfo   
local master "`r(pdir)'"
local doname "`r(dofile)'" 

cd "../../Data/Clean/ENH"
* cd C:/Users/JorgeEduardo/Dropbox/AA_Minimum_Wage/Data/Clean/ENH
* cap log close
* log using "C:/Users/JorgeEduardo/Dropbox/AA_Minimum_Wage/Code/Clean/merge_ocupados.log", text replace
* Obtener una lista de variables comunes en todas las bases de datos
forvalues year=1996(1)2000 {
	forvalues trim=1(1)4 {
		di "`year' - `trim'"
		project, uses("`year'/TRIM `trim'/`year'_`trim'_general.dta")
		use "`year'/TRIM `trim'/`year'_`trim'_general.dta" , clear
		project, uses("`year'/TRIM `trim'/`year'_`trim'_fuerza_trabajo.dta") preserve
		merge 1:1 id orden using "`year'/TRIM `trim'/`year'_`trim'_fuerza_trabajo.dta", keep(match)
		ren _merge _merge1
		project, uses("`year'/TRIM `trim'/`year'_`trim'_ocupados.dta") preserve
		* Notice that I am keeping both labor force and occupied here. This is because I intend to build city level employment rates afterwards.
		merge 1:1 id orden using "`year'/TRIM `trim'/`year'_`trim'_ocupados.dta", keep(match master)
		ren _merge _merge2
		gen ocupado=_merge2
		gen year=`year'
		gen trim=`trim'
		gen time=yq(year,trim)
		format time %tq
		tempfile b`year'`trim'
		save `b`year'`trim''
	}
}

clear
forvalues year=1996(1)2000 {
	forvalues trim=1(1)4 {
	di "`year' - `trim' " 
		append using `b`year'`trim''
	}
}
save "`master'/Data/Source/ocupados.dta", replace
project, creates("`master'/Data/Source/ocupados.dta")
* log close